package com.danicsoft.daide.serverclient;

import com.danicsoft.daide.command.UnitPos;
import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.Token;
import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap.Coasts;
import com.danicsoft.daide.token.TokenMap.UnitTypes;

import dip.world.Coast;
import dip.world.Location;
import dip.world.Power;
import dip.world.Province;
import dip.world.Unit;
import dip.world.World;
import dip.world.Unit.Type;

/**
 * Contains a collection of functions for converting back between firefly types
 * and jDip types.
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class jDipAdapter {
	/**
	 * Converts a Token representing a power to a jDip class representing a
	 * power.
	 * 
	 * @param power
	 *        A StandardToken representing the power to convert.
	 * @param world
	 *        The jDip world that we use to convert.
	 * @return A jDip Power object
	 */
	public static Power convertPower(StandardToken power, World world) {
		return world.getMap().getPower(power.toString());
	}

	/**
	 * Convert a UnitPos object to a Location object.
	 * 
	 * @param position
	 *        The province location.
	 * @param world
	 *        The world to convert.
	 * @return A Location object.
	 */
	public static Location convertLocation(Token position, World world) {
		Coast coast;
		Province prov;
		if (position.isGroup()) {
			TokenGroup tGroup = (TokenGroup) position.clone();
			coast = convertCoast((StandardToken) tGroup.next());
			prov = convertProvince((StandardToken) tGroup.next(), world);
		}
		else {
			prov = convertProvince((StandardToken) position, world);
			if (prov.isLand())
				coast = Coast.LAND;
			else
				coast = Coast.SINGLE;
		}

		return new Location(prov, coast);
	}

	/**
	 * Converts a Token representing a province to a jDip class representing a
	 * Province.
	 * 
	 * @param province
	 *        A StandardToken representing a province.
	 * @param world
	 *        The world to use to convert.
	 * @return A jDip Province Object.
	 */
	public static Province convertProvince(StandardToken province, World world) {
		return world.getMap().getProvince(province.toString());
	}

	/**
	 * Converts a Token representig a coast to a jDip class representing a
	 * coast.
	 * 
	 * @param coast
	 *        A StandardToken representing a coast.
	 * @return A jDip Coast object.
	 */
	public static Coast convertCoast(StandardToken coast) {
		switch (coast.getSubType()) {
			case Coasts.NEC:
			case Coasts.NCS:
				return Coast.NORTH;
			case Coasts.NWC:
			case Coasts.WCS:
				return Coast.WEST;
			case Coasts.SWC:
			case Coasts.SCS:
				return Coast.SOUTH;
			case Coasts.SEC:
			case Coasts.ECS:
				return Coast.EAST;

		}
		return Coast.UNDEFINED;
	}

	/**
	 * Convert the unit position given to a jDip Type object
	 * 
	 * @param position
	 *        The position to convert.
	 * @return A jDip Type object.
	 */
	public static Type convertType(UnitPos position) {
		if (position.unit_type.getSubType() == UnitTypes.AMY) {
			return Unit.Type.ARMY;
		}
		return Unit.Type.FLEET;
	}
}
